<template>
  <div>
    <el-form
      :model="updateForm"
      :rules="rules"
      ref="updateForm"
      label-width="100px"
      class="demo-ruleForm"
      @submit.native.prevent
    >
      <el-row>
        <el-row>
          <el-col :xs="24" :sm="12" :md="12">
            <el-form-item label="产品名称" prop="flowName">
              <el-input v-model="updateForm.flowName" placeholder="请输入产品名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12">
            <el-form-item label="当前版本" prop="flowVersion">
              <el-input v-model="updateForm.flowVersion" readonly="true" placeholder="当前版本" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :sm="12" :md="12">
            <el-form-item label="产品编码" prop="flowCode">
              <el-input v-model="updateForm.flowCode" placeholder="请输入产品编码" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12">
            <el-form-item label="场景编码" prop="sceneCode">
              <el-input v-model="updateForm.sceneCode" placeholder="请输入场景编码" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :sm="12" :md="12">
            <el-form-item label="是否默认版本" prop="disabled">
              <el-checkbox v-model="updateForm.isDefault" :true-label="1" :false-label="0">默认版本</el-checkbox>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :sm="12" :md="12">
            <el-form-item label="执行模式" prop="asyncMode">
              <el-radio v-model="updateForm.asyncMode" label="1" value="1">同步</el-radio>
              <el-radio v-model="updateForm.asyncMode" label="2" value="2">半异步</el-radio>
              <el-radio v-model="updateForm.asyncMode" label="3" value="3">全异步</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :sm="12" :md="12">
            <el-form-item label="产品状态" prop="disabled">
              <el-radio v-model="updateForm.disabled" :label="1" value="1">禁用</el-radio>
              <el-radio v-model="updateForm.disabled" :label="0" value="0">启用</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
      </el-row>

    </el-form>
  </div>
</template>
<script>
  import HttpCode from '@/js/httpCode.js'
  import engineTbFlowApi from '@/apis/dpsApp/engineTbFlowApi.js'

  export default {
    name: 'EngineFlowUpdate',
    components: {
      SelectTree: () => import('@/components/selecttree/SelectTree.vue')
    },
    data () {
      return {
        updateForm: {
          id: '',
          asyncMode: '3',
          disabled: 0,
          flowCode: '',
          flowName: '',
          flowVersion: '',
          isDefault: 1,
          sceneCode: '01',
        },
        deptProps: {
          children: 'children',
          label: 'deptName'
        },
        rules: {
          // flowName: [
          //   {required: true, message: '请输入产品名称', trigger: 'blur'},
          //   {min: 2, max: 32, message: '产品名称长度2-32字符', trigger: 'blur'}
          // ],
          // flowCode: [
          //   {required: true, message: '请输入用户昵称', trigger: 'change'}
          // ],
          // sceneCode: [
          //   {required: true, message: '请输入用户昵称', trigger: 'change'}
          // ]
        }
      }
    },
    created () {
      //可能需要特殊处理，手动赋值
      if (this.data != null) {
        console.log(this.data)
        this.updateForm = this.data
      }
    },
    methods: {
      submitForm () {
        this.$refs['updateForm'].validate(valid => {
          if (valid) {
            this.submitFormHandle()
          } else {
            this.cancelSubmitForm()
          }
        })
      },
      submitFormHandle () {
        if (this.updateForm.id == undefined || this.updateForm.id == '') {
          engineTbFlowApi
            .addService(this.updateForm)
            .then(msg => {
              this.submitFormSuccess(msg)
            })
            .catch(msg => {
              if ((msg.cdoe = 400)) {
                this.formError(msg)
              }
              this.cancelSubmitForm()
            })
        } else {
          engineTbFlowApi.updateService(this.updateForm)
            .then(msg => {
              this.submitFormSuccess(msg)
            }).catch(msg => {
            if ((msg.cdoe = 400)) {
              this.formError(msg)
            }
            this.cancelSubmitForm()
          })
        }
      },
      submitFormSuccess (msg) {
        this.$store.commit('successSubmitForm', {
          name: this.formKey,
          data: msg
        })
      },
      cancelSubmitForm () {
        this.$store.commit('cancelSubmitForm', {name: this.formKey})
      },
      formError (msg) {
        if (msg.code == HttpCode.E400) {
          for (let row of msg.errors) {
            let fields = this.$refs['updateForm'].fields.filter(it => {
              return it.prop == row.errName
            })
            if (fields.length > 0) {
              fields[0].validateMessage = row.errMsg
              fields[0].validateState = 'error'
            }
          }
        }
      }
    },
    props: {
      formKey: {
        type: String,
        default: 'currentForm'
      },
      data: {
        type: Object,
        default: {}
      }
    },
    computed: {
      submitFormEventListener () {
        return this.$store.getters.onEvent(`${this.formKey}-submitForm`)
      }
    },
    watch: {
      submitFormEventListener: function (a, b) {
        if (a == null || a == b) {
          return null
        }
        this.submitForm()
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
  }

  .avatar {
    width: 120px;
    height: 120px;
    display: block;
  }
</style>
